import requests
import time
import random
from jsonpath import jsonpath
import json
import pymysql

'''
这里是20秒执行一次掌上高考
'''
class T_X_recruit():
    def __init__(self):
        self.url = "https://api.zjzw.cn/web/api/?keyword=&level1=1&level2=&level3=&page=3&size=30&sort=&uri=apidata/api/gkv3/special/lists&signsafe=c9bcea8f63c7e5b2885d161afc059a74"
        self.headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        # 'Referer': 'https://careers.tencent.com/m/search.html?query=ot_40001001,ot_40001002,ot_40001003,ot_40001004,ot_40001005,ot_40001006',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36'
        }
        self.db = pymysql.connect(host='192.168.133.4', user='root', password='123456', database='self_student_jobs')
        self.cursor = self.db.cursor()

    def get_html(self, url):
        response = requests.get(url=url, headers=self.headers)
        if response.status_code == 200:
            # print(response.text)
            return response.text
        raise Exception('No data~')


    def parse_data(self, json_data):
        for item in json_data['data']['item']:
            data = {
                'spcode': item['spcode'],
                'hightitle': item['hightitle'],
                'level2_name': item['level2_name'],
                'level3_name': item['level3_name'],
                'limit_year': item['limit_year'],
                'level1_name': item['level1_name'],
                'level2_name': item['level2_name'],
                'degree': item['degree']
            }
            print(data)
            self.save_data(data)


    def save_data(self, data):
        sql = 'INSERT INTO tx_majors (spcode, hightitle, level2_name, level3_name, limit_year, level1_name, degree) VALUES (%s, %s, %s, %s, %s, %s, %s)'
        self.cursor.execute(sql, (
        data['spcode'], data['hightitle'], data['level2_name'], data['level3_name'], data['limit_year'], data['level1_name'],data['degree']))
        # print("——————————————————————————————正在插入数据————————————————————————————————")
        self.db.commit()

    def main(self):
        for i in range(1, 3):
            url = f"https://api.zjzw.cn/web/api/?keyword=&level1=1&level2=&level3=&page={i}&size=30&sort=&uri=apidata/api/gkv3/special/lists&signsafe=c9bcea8f63c7e5b2885d161afc059a74"
            t = random.randint(20, 30)
            time.sleep(t)  # 延时请求
            html_data = self.get_html(url)
            json_data = json.loads(html_data)
            # print(json_data)
            self.parse_data(json_data)
            print(f"========下面是第{i + 1}页=========")

        #self.db.close()


t_x_recruit = T_X_recruit()
t_x_recruit.main()
